<template>
<h2>计算器练习</h2>
  数字1:<input type="text" v-model="n1"><br>
  数字2:<input type="text" v-model="n2"><br>
  运算:<input type="button" value="加" @click="f('+')">&nbsp;&nbsp;
  <input type="button" value="减" @click="f('-')">&nbsp;&nbsp;
  <input type="button" value="乘" @click="f('*')">&nbsp;&nbsp;
  <input type="button" value="除" @click="f('/')">&nbsp;&nbsp;
  <h3>{{result}}</h3>
</template>

<script setup>
import {ref} from "vue";

const n1 = ref('');
const n2 = ref('');
const result = ref('');
const f = (x) =>{
  // eval(字符串) 此方法可以将字符串以JS代码的形式运行,并得到结果
  result.value = eval(n1.value+x+n2.value);
}
</script>

<style scoped>

</style>